package uk.co.q3c.deplan.client.domain.dag;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Id;
import javax.persistence.Transient;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Cached;
import com.googlecode.objectify.annotation.Parent;
import com.googlecode.objectify.annotation.Serialized;
import com.googlecode.objectify.annotation.Unindexed;

/**
 * Records the changes to a common or shared structure
 * 
 * @author David Sowerby 30 Jul 2010
 * 
 */

@Unindexed
@Cached(expirationSeconds = 600)
public class SharedStructureChangeLog {

	@Serialized
	private final List<SharedStructureChange> changes = new ArrayList<SharedStructureChange>();
	@Parent
	private Key<SharedStructure> parent;
	@Id
	private Long keyId;
	private int parentVersion;
	@Transient
	private boolean suspended;

	public Long getKeyId() {
		return keyId;
	}

	public void setKeyId(Long keyId) {
		this.keyId = keyId;
	}

	public Key<SharedStructure> getParent() {
		return parent;
	}

	public void setParent(Key<SharedStructure> parent) {
		this.parent = parent;
	}

	public int getParentVersion() {
		return parentVersion;
	}

	public void setParentVersion(int parentVersion) {
		this.parentVersion = parentVersion;
	}

	public void add(SharedStructureChange change) {
		if (!suspended) {
			changes.add(change);
		}
	}

	public int changeCount() {
		return changes.size();
	}

	public void clear() {
		changes.clear();
	}

	public SharedStructureChange get(int index) {
		return changes.get(index);
	}

	public List<SharedStructureChange> getChanges() {
		return changes;
	}

	public void suspendLogging() {
		suspended = true;
	}

	public void resumeLogging() {
		suspended = false;
	}

	public boolean isSuspended() {
		return suspended;
	}
}